<?php

namespace IDock\Insurance\MACN\Response;

use IDock\Http\Response;
use IDock\Printer\EPolicyDownloadPrinter;
use IDock\Printer\ProposalRecallPrinter;

class EPolicyDownloadResponse extends AllMACNResponse
{
    public $body = [];

    public $biUrl = '';

    public $ciUrl = '';

    public $noCarUrl = '';

    public $biPolicy = '';

    public $ciPolicy = '';

    public function printerInit(): Response
    {
        $this->body = $this->getBody();
        if(!empty($this->getError())){
            return $this;
        }
        foreach ($this->body['imgNodes']['ImgNodeVo'] as $val){
            if(strpos($val['imgName'],'机动车商业保险保险单') !== false || strpos($val['imgName'],'新能源汽车商业保险保险单') !== false){
                $this->biUrl = $val['imgURL'] ?? '';
            }
            if(strpos($val['imgName'],'机动车交通事故责任强制保险单') !== false){
                $this->ciUrl = $val['imgURL'] ?? '';
            }
        }

        $proposalRecallPrinter = new EPolicyDownloadPrinter();
        $proposalRecallPrinter->doing = true;
        if(!empty($this->biPolicy) && !empty($this->ciPolicy)){
            if(!empty($this->biUrl) && !empty($this->ciUrl)){
                $proposalRecallPrinter->finish = true;
                $proposalRecallPrinter->doing = false;
            }
        }else{
            if(!empty($this->biPolicy) && !empty($this->biUrl)){
                $proposalRecallPrinter->finish = true;
                $proposalRecallPrinter->doing = false;
            }elseif (!empty($this->ciPolicy) && !empty($this->ciUrl)){
                $proposalRecallPrinter->finish = true;
                $proposalRecallPrinter->doing = false;
            }
        }

        $this->printer = $proposalRecallPrinter;
        return  $this;
    }

    public function setNoCar($noCarNo)
    {
        if(!empty($this->getError())){
            return $this;
        }
        foreach ($this->body['imgNodes']['ImgNodeVo'] as $val){
            if(strpos($val['imgName'],$noCarNo) !== false){
                $this->noCarUrl = $val['imgURL'] ?? '';
                if($this->noCarUrl){
                    $this->printer->finish = true;
                }
            }
        }
        return $this;
    }

}
